package do1.mobo.widget;

import java.util.ArrayList;
import java.util.List;
import do1.common.json.JSONArray;
import do1.common.json.JSONObject;
import do1.common.util.JsonUtil;
import do1.mobo.constant.Constant;
import do1.mobo.graphics.ImageFactory;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;

public class GridViewAdapter extends BaseAdapter
{
	private Context mContext;
	private List<GridViewItem> gridViewItemList=null;
	final private int GRID_ITEM_PADDING=8;
//	private boolean forGridView=true;
	
//	public void setForGridView(boolean forGridView)
//	{
//		this.forGridView = forGridView;
//	}

	public GridViewAdapter(Context c)
	{
		mContext = c;
	}
	
	public void parse(JSONObject json,ImageFactory imageFactory)
	{
//		JSONArray names=json.names();
//		JSONArray arr=JsonUtil.toJsonArray(json, names);
//		int length=arr.length();
//		for(int i=0;i<length;i++)
//		{
//			String key=JsonUtil.getString(names,i);
//
//			if(key.equalsIgnoreCase(Constant.JSON_KEY_ITEMS))
//			{
//				JSONArray itemArray=JsonUtil.getJsonArray(arr,i);
//				int count=itemArray.length();
//				for(int j=0;j<count;j++)
//				{
//					JSONObject itemObj=JsonUtil.getJsonObject(itemArray, j);
//					GridViewItem item=new GridViewItem(mContext);
//					item.parse(itemObj, imageFactory);
//					//if(forGridView)
//					//{
//						item.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));//85,85
//					//}
//						
//					item.setPadding(GRID_ITEM_PADDING,GRID_ITEM_PADDING,GRID_ITEM_PADDING,GRID_ITEM_PADDING);
//					addGridViewItem(item);
//					
////					if(!forGridView)
////					{
////						item.setOnClickListener(OnClickListenerEvent.getInstance());
////					}
//				}
//			}
//		}		
		
		JSONArray itemArray=JsonUtil.getJsonArray(json,Constant.JSON_KEY_ITEMS);
		if(itemArray!=null)
		{
			int count=itemArray.length();
			for(int j=0;j<count;j++)
			{
				JSONObject itemObj=JsonUtil.getJsonObject(itemArray, j);
				GridViewItem item=new GridViewItem(mContext);
				item.parse(itemObj, imageFactory);
				//if(forGridView)
				//{
					item.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));//85,85
				//}
					
				item.setPadding(GRID_ITEM_PADDING,GRID_ITEM_PADDING,GRID_ITEM_PADDING,GRID_ITEM_PADDING);
				addGridViewItem(item);
				
	//			if(!forGridView)
	//			{
	//				item.setOnClickListener(OnClickListenerEvent.getInstance());
	//			}
			}
		}
	}
	
	public void addGridViewItem(GridViewItem item)
	{
		if(gridViewItemList==null)
			gridViewItemList=new ArrayList<GridViewItem>();
		
		gridViewItemList.add(item);
	}
	
	public int getCount()
	{
		if(gridViewItemList==null)
			return 0;
		
		int count=gridViewItemList.size();   
		return count;
	}

	public Object getItem(int position,String str)
	{
		if(gridViewItemList==null)
			return null;
		
		int count=gridViewItemList.size();
		if(position<0 && position>=count)
		{
			return null;
		}
		
		GridViewItem item=gridViewItemList.get(position);
		return item;
	}
	
	public Object getItem(int position)
	{
		return position;
	}

	public long getItemId(int position)
	{
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent)
	{
		GridViewItem item=null;
		if (convertView == null)
		{
			item=(GridViewItem)getItem(position,"");
		}
		else
		{
			item = (GridViewItem) convertView;
		}
//		test
//		TextView item=new TextView(mContext);
//		item.setText("item"+position);

		return item;

	}
}
